/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : kf_ic.c                                               *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $Hard Version     : KF32A156-MINI-EVB_V1.2                                *
 *  $Description$     : Main Interrupt Service Routines.                      *      
 *                      This file provides template for all exceptions        *
 *                      handler and peripherals interrupt service routine.    *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyright protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.02.00 FAE Group     Version 2.0 update                      *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
**                          Include Files                                    **
******************************************************************************/
#include "system_init.h"
#include "SPI.h"
#include <stdio.h>
 uint8_t count=0;
/*******************************************************************************
**                   KF32A156 Processor Exceptions Handlers  		         **
*******************************************************************************/
extern uint8_t Spi_Length;
//*****************************************************************************************
//                                 NMI Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _NMI_exception(void)
{
}

//*****************************************************************************************
//                               HardFault Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _HardFault_exception(void)
{
}

//*****************************************************************************************
//                               StackFault Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _StackFault_exception(void)
{
}

//*****************************************************************************************
//                               SVC Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SVC_exception(void)
{
}

//*****************************************************************************************
//                              SoftSV Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SoftSV_exception(void)
{
}

//*****************************************************************************************
//                              SysTick Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SysTick_exception(void)
{
}

//*****************************************************************************************
//                             SPI2 Interrupt Course
//*****************************************************************************************
void __attribute__((interrupt)) _SPI0_exception(void)
{
	/* Waiting for the sending buffer to be empty */
	if(SPI_Get_Transmit_Buf_Flag(SPI0_SFR)==RESET)
	{
		/* Polling to send string  */
		if(Spi_Length < SPI_TRANSMISSION_DATA_SIZE)
		{
			/* Send data */
			SPI_I2S_SendData8(SPI0_SFR,Tx_Master[Spi_Length]);
			/* Waiting to accept that the buffer is not empty */
			while(!SPI_Get_Receive_Buf_Flag(SPI0_SFR));
			/* Read the received data */
			Spi_ReadData[Spi_Length] = SPI_I2S_ReceiveData(SPI0_SFR);
			Spi_Length++;
			/* If it is full, use the serial port to print out the received data */
			if(Spi_Length == SPI_TRANSMISSION_DATA_SIZE)
			{
				/* Disable the send buffer is empty interrupt */
				SPI_TNEIE_INT_Enable(SPI0_SFR,FALSE);
				/* Waiting for spi to send no data */
				while(SPI_Get_BUSY_Flag(SPI0_SFR)==SET);
				Spi_Length = 0;

				fprintf(USART4_STREAM,"%s\r\n", Spi_ReadData);
				count++;
				/* LED2 flashing */
				if(count>=50)
				{
					GPIO_Toggle_Output_Data_Config(GPIOC_SFR, GPIO_PIN_MASK_6);
					count=0;
				}
				/* Enable the send buffer is empty interrupt */
				SPI_TNEIE_INT_Enable(SPI0_SFR,TRUE);
			}

		}
	}
}
